package com.kc.data.scenic.controller;

/**
 * @ClassName BizScenicController
 * @Description 景区信息Controller
 * @Author wangdongliang
 * @Date 2021/6/16 11:12
 */

import com.kc.data.common.annotation.Log;
import com.kc.data.common.base.StringUtils;
import com.kc.data.common.core.AjaxResult;
import com.kc.data.common.domain.model.BizScenic;
import com.kc.data.common.enums.BusinessType;
import com.kc.data.common.service.IBizScenicService;
import com.kc.data.common.service.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/business/scenic")
@Slf4j
public class BizScenicController {
    @Autowired
    private IBizScenicService bizScenicService;
    @Resource
    private ImageService imageService;
    public static final Integer DELETE = 1;

    /**
     * 获取景区详情
     * @param scenicId
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('business:scenic:info')")
    @GetMapping("/info/{scenicId}")
    public AjaxResult getInfoByUserId(@PathVariable Integer scenicId) {
        BizScenic bizScenic = bizScenicService.getInfoById(scenicId);
        if(bizScenic.getScenicImgs() == null){
            String[] arr = new String[0];
            bizScenic.setScenicImgs(arr);
        }
        if(StringUtils.isNotEmpty(bizScenic.getScenicImg())){
            bizScenic.setScenicImgs(StringUtils.split(bizScenic.getScenicImg(),","));
        }
        return AjaxResult.success(bizScenic);
    }

    /**
     * 编辑景区
     */
    //@Log(title = "景区管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@Validated @RequestBody BizScenic bizScenic) {
        //图片数组转字符串逗号分隔
        bizScenic.setScenicImg(StringUtils.join(bizScenic.getScenicImgs(), ","));
        //处理列表图片宽高
        bizScenic.setImageAttr(imageService.getImageSizeToJson(bizScenic.getScenicImgs()));
        bizScenic.setGoodsId(integerArrayToString(bizScenic.getGoodsIdArray()));
        return AjaxResult.success(bizScenicService.edit(bizScenic));
    }

    private String integerArrayToString(Integer[] arr) {
        if (ArrayUtils.isNotEmpty(arr)) {
            StringBuilder sb = new StringBuilder();
            for (Integer item : arr) {
                sb.append(item);
                sb.append(",");
            }
            String result = sb.toString();
            if (result.length() > 0) {
                result = result.substring(0, result.length() - 1);
            }
            return result;
        }
        return null;
    }
}
